﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmlStudio.Logger.Interfaces;

namespace XmlStudio.Logger
{
    /// <summary>
    /// Creates LogWritter instance.
    /// </summary>
    public class LogFactory : ILogFactory
    {
        #region Fields
        private static ILogWriter logWriter;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the LogFactory class.
        /// </summary>
        internal LogFactory()
        {
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates log writer instance.
        /// </summary>
        /// <param name="path">Path to the desired log file.</param>
        /// <returns>Reference to ILogWriter.</returns>
        public ILogWriter Create(string path)
        {
            logWriter = new TextLogWriter(path);
            return logWriter;
        }

        /// <summary>
        /// Gets or sets the current log writer.
        /// </summary>
        public ILogWriter Current
        {
            get
            {
                if (logWriter == null)
                {
                    throw new ApplicationException("logWriter is null");
                }
                else
                {
                    return logWriter;
                }
            }
            set
            {
                logWriter = value;
            }
        }

        /// <summary>
        /// Disposes of current log writer. Sets Current to null.
        /// </summary>
        public void DisposeOfLogWriter()
        {
            if (logWriter != null)
            {
                logWriter.Dispose();
                logWriter = null;
            }
        }
        #endregion
    }
}
